﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AppManager
{
    public partial class FrmNewCard : Form
    {
        public FrmNewCard()
        {
            InitializeComponent();
            LoadTableList();
        }

        private void LoadTableList()
        {
            using (CRMDataContext dataContext = new CRMDataContext())
            {
                ddlTableList.DataSource = dataContext.AppObjects.Where(obj => (obj.ObjectType == "TABLE")).ToList();
                ddlTableList.DisplayMember = "Name";
                ddlTableList.ValueMember = "Name";
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            MetaCardPage cardPage = new MetaCardPage()
            {
                GlobalScripts = new List<MetaScript>(),
                Description = "",
                LinkTable = ddlTableList.Text,
                Name = txtName.Text,
                FieldGroups = new List<MetaFieldGroup>()
            };

            foreach (DataGridViewRow row in GridFields.Rows)
            {
                if (row.Cells[0].Value != null)
                {
                    MetaFieldGroup group = new MetaFieldGroup()
                    {
                        Name = row.Cells[0].Value.ToString(),
                        Type = row.Cells[1].Value.ToString() == "Linear" ? FieldGroupType.Linear : FieldGroupType.Grid,
                        Fields = new List<MetaFormField>(),
                        LinkedKey = "",
                        LinkTable = ""
                    };

                    string[] fieldList = row.Cells[2].Value.ToString().Split(';');
                    foreach (string item in fieldList)
                    {
                        if (!string.IsNullOrWhiteSpace(item))
                        {
                            group.Fields.Add(new MetaFormField()
                            {
                                ClientScripts = new List<MetaScript>(),
                                Name = item
                            });
                        }
                    }

                    cardPage.FieldGroups.Add(group);
                }
            }

            string formXml = Utility.SerializeObject(cardPage);
            AppObject createAppObject = new AppObject()
            {
                CreatedDate = DateTime.Now,
                Metadata = formXml,
                ModuleID = Convert.ToInt32(txtModuleID.Text),
                Name = txtName.Text.ToUpper().Trim(),
                ObjectType = "CARD",
                LinkTable = ddlTableList.Text.ToUpper()
            };

            using (CRMDataContext dataContext = new CRMDataContext())
            {
                dataContext.AppObjects.InsertOnSubmit(createAppObject);
                dataContext.SubmitChanges();
            }
        }

        private void ddlTableList_SelectedIndexChanged(object sender, EventArgs e)
        {
            string tableSelected = ddlTableList.Text;
            if (!string.IsNullOrWhiteSpace(tableSelected))
            {
                using (CRMDataContext dataContext = new CRMDataContext())
                {
                    string tableXml = dataContext.AppObjects.Where(obj => (obj.ObjectType == "TABLE" && obj.Name == tableSelected.ToUpper())).ToList()[0].Metadata;
                    MetaTable tableObject = Utility.DeserializeString<MetaTable>(tableXml) as MetaTable;
                    string fieldList = "";
                    foreach (var item in tableObject.Fields)
                    {
                        fieldList += item.Name + ";";
                    }

                    txtFieldList.Text = fieldList;
                }
            }
        }
    }
}
